package com.ews.onlineexchange.model;

import java.io.File;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Lob;
import javax.persistence.PrePersist;
import javax.persistence.Transient;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import com.ews.onlineexchange.util.Config;

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Attachment extends DataEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String 			filename;
    private byte[] 			filedata;
    private long 			filesize;
    private String 			filepath;
    private String 			contentType;
    private String 			diskFileName;

    @NotNull
	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}
	
	@NotNull
	@Min(1L)
	public long getFilesize() {
		return filesize;
	}

	public void setFilesize(long filesize) {
		this.filesize = filesize;
	}
	
	@Lob
	@Basic(fetch = FetchType.LAZY)
//	@Transient
	public byte[] getFiledata() {
		return filedata;
	}

	public void setFiledata(byte[] filedata) {
		this.filedata = filedata;
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getFilepath() {
		return filepath;
	}

	public void setFilepath(String filepath) {
		this.filepath = filepath;
	}
	
	
	public String getDiskFileName() {
		return diskFileName;
	}

	public void setDiskFileName(String diskFileName) {
		this.diskFileName = diskFileName;
	}
	
	@PrePersist
	public void prePersist() {
		super.prePersist();
		if (getFilename().indexOf(".") > -1) {
			setContentType(getFilename().substring(getFilename().lastIndexOf(".")));
		}
		this.setFiledata( null );
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((contentType == null) ? 0 : contentType.hashCode());
		result = prime * result + ((filename == null) ? 0 : filename.hashCode());
		result = prime * result + ((filepath == null) ? 0 : filepath.hashCode());
		result = prime * result + (int) (filesize ^ (filesize >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		Attachment other = (Attachment) obj;
		if (contentType == null) {
			if (other.contentType != null)
				return false;
		} else if (!contentType.equals(other.contentType))
			return false;
		if (filename == null) {
			if (other.filename != null)
				return false;
		} else if (!filename.equals(other.filename))
			return false;
		if (filepath == null) {
			if (other.filepath != null)
				return false;
		} else if (!filepath.equals(other.filepath))
			return false;
		if (filesize != other.filesize)
			return false;
		return true;
	}

	public Attachment() {}

	@Transient
	public File getFile() {
		String fullFilepath = Config.getStaticProperty( "com.ews.onlineExchange.saveDir" ).concat( this.getFilepath() );
		return new File( fullFilepath );
	}
	
	@Transient
	public String getFileURI() {
		return Config.getStaticProperty( "com.ews.onlineExchange.uploadContextPath" ).concat( "/" ).concat( this.getFilepath() );
	}

}
